//
//  MainViewController.m
//  HLZhiHuDaily
//
//  Created by 胡林 on 16/3/1.
//  Copyright © 2016年 胡林. All rights reserved.
//

#import "MainViewController.h"
//MainViewScaleYEndValue
#define MainViewMoveXMaxValue       ABS(kScreenWidth*0.6 - 0)
#define MainViewScaleMaxValue       ABS(1 - 1)

#define LeftViewScaleMaxValue       ABS(1 - 1)
#define LeftViewMoveXMaxValue       ABS(1 - 1)
@interface MainViewController ()

@end

@implementation MainViewController
-(instancetype)initWithLeftMenuViewController:(DrawerViewController *)menuVC andHomeViewController:(HomeViewController *)homeVC{
    self = [super init];
    if (self) {
        self.homeViewController = homeVC;
        self.leftMenuViewController = menuVC;
        
    }return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _distance = 0;
    self.leftMenuView = self.leftMenuViewController.view;
    self.leftMenuView.frame = kScreenBounds;
    self.leftMenuView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, 1), CGAffineTransformTranslate(CGAffineTransformIdentity, -kScreenWidth, 0));
    [self.view addSubview:self.leftMenuView];
    self.mainView = [[UIView alloc] initWithFrame:kScreenBounds];
    [self.view addSubview:self.mainView];
    [_mainView addSubview:self.homeViewController.view];
    UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.mainView addGestureRecognizer:panGes];
    self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
}

//点击关闭抽屉
- (void)tap:(UITapGestureRecognizer *)recongizer {
    [self showMainView];
}
//轻扫关闭抽屉
-(void)showMainView{
    [UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        _mainView.transform = CGAffineTransformIdentity;
        _leftMenuView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, 1), CGAffineTransformTranslate(CGAffineTransformIdentity, -kScreenWidth*0.6, 0));
    } completion:^(BOOL finished) {
        _distance = 0;
        [_mainView removeGestureRecognizer:_tap];
    }];
    
    
}

//轻扫打开抽屉
-(void)showLeftMenuView{
    [UIView animateWithDuration:0 delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        _mainView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, 1), CGAffineTransformTranslate(CGAffineTransformIdentity, kScreenWidth*0.6, 0));
        _leftMenuView.transform =CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        _distance = kScreenWidth*0.6;
        [_mainView addGestureRecognizer:_tap];
    }];
}
//平移（平移打开或是关闭抽屉）
- (void)pan:(UIPanGestureRecognizer *)recongnizer {
    
    CGFloat moveX = [recongnizer translationInView:self.view].x;
    CGFloat truedistance = _distance + moveX;
    CGFloat percent = truedistance/MainViewMoveXMaxValue;
    if (truedistance >= 0 && truedistance <= MainViewMoveXMaxValue) {
        _mainView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1, 1-MainViewScaleMaxValue*percent), CGAffineTransformTranslate(CGAffineTransformIdentity, 0+truedistance, 0));
        _leftMenuView.transform = CGAffineTransformConcat(CGAffineTransformScale(CGAffineTransformIdentity, 1+LeftViewScaleMaxValue*percent, 1+LeftViewScaleMaxValue*percent), CGAffineTransformTranslate(CGAffineTransformIdentity, -kScreenWidth*0.6+LeftViewMoveXMaxValue*percent, 0));
    }
    if (recongnizer.state == UIGestureRecognizerStateEnded) {
        if (truedistance <= MainViewMoveXMaxValue/2) {
            [self showMainView];
        }else{
            [self showLeftMenuView];
        }
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
